package com.situ.mall.admin.controller;

import com.situ.mall.admin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yangkx
 * @create 2023-09-11-14:45
 */
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private IUserService userService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    //登陆过滤
    @PostMapping("/login")
    public String login(String username, String password){
        boolean b = userService.check(username,password);
        if (b){
            return "redirect:/";
        }else {
            return "login";
        }
    }

    //登出 删除session
    @GetMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        response.sendRedirect("/login");

    }

}
